//
//  YCRegisterImprovePhotoPickerView.m
//  YCSchool
//
//  Created by Yanglixia on 16/5/8.
//  Copyright © 2016年 yanglinxia. All rights reserved.
//

#import "YCRegisterImprovePhotoPickerView.h"
#import "YCRegisterImprovePhotoButton.h"
#import "MultipleImagePickerViewController.h"

@interface YCRegisterImprovePhotoPickerView ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>

/** 背景 */
@property (nonatomic, strong) UIButton *actionBackView;
/** sheet */
@property (nonatomic, strong) UIView *actionView;

/** 点击拍照回调 */
@property (nonatomic, copy) YCTakePhoteClickedAction taktePhotoBlock;
/** 选择相册回调 */
@property (nonatomic, copy) YCSelectPhoteClickedAction selectPhoteBlock;
/** 取消回调 */
@property (nonatomic, copy) YCCancelClickedAction cancelBlock;

@end


@implementation YCRegisterImprovePhotoPickerView

- (void)showWithTakePhotoClickBlock:(YCTakePhoteClickedAction)takePhotoBlock selectPhotoClickBlock:(YCSelectPhoteClickedAction)selectPhotoBlcok cancelBlock:(YCCancelClickedAction)cancelBlock
{
    [self show];//弹出
    _taktePhotoBlock = takePhotoBlock;
    _selectPhoteBlock = selectPhotoBlcok;
    _cancelBlock = cancelBlock;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
        backButton.backgroundColor = [UIColor clearColor];
        [backButton addTarget:self action:@selector(cancelBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:backButton];
    
        self.actionBackView = backButton;
    
    
        UIView *actionView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight, kScreenWidth, 255)];
        actionView.backgroundColor = [UIColor whiteColor];
        [backButton addSubview:actionView];
        self.actionView = actionView;
        // 拍照
        YCRegisterImprovePhotoButton *takePhoteBtn = [[YCRegisterImprovePhotoButton alloc] init];
        takePhoteBtn.frame = CGRectMake(56, 56, 60, 80);
        [takePhoteBtn setImage:[UIImage imageNamed:@"拍照"] forState:UIControlStateNormal];
        [takePhoteBtn setTitle:@"拍照" forState:UIControlStateNormal];
        [takePhoteBtn setTitleColor:[UIColor yc_colorWithUInt:0x808080] forState:UIControlStateNormal];
        [takePhoteBtn addTarget:self action:@selector(takePhoteBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [actionView addSubview:takePhoteBtn];
        
        // 相册
        YCRegisterImprovePhotoButton *selectPhoteBtn = [[YCRegisterImprovePhotoButton alloc] init];
        selectPhoteBtn.frame = CGRectMake(kScreenWidth - 116, 56, 60, 80);
        [selectPhoteBtn setImage:[UIImage imageNamed:@"选择相册"] forState:UIControlStateNormal];
        [selectPhoteBtn setTitleColor:[UIColor yc_colorWithUInt:0x808080] forState:UIControlStateNormal];
        
        [selectPhoteBtn setTitle:@"相册" forState:UIControlStateNormal];
        [selectPhoteBtn addTarget:self action:@selector(selectPhoteBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [actionView addSubview:selectPhoteBtn];
        
        // 取消
        UIButton *cancelButton = [[UIButton alloc] initWithFrame:CGRectMake(47,179, kScreenWidth - 94, 36)];
        cancelButton.titleLabel.font = [UIFont systemFontOfSize:15];
        cancelButton.backgroundColor = [UIColor yc_colorWithUInt:0x3d3e40];
        [cancelButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
        cancelButton.layer.cornerRadius = 17.5;
        cancelButton.layer.masksToBounds = YES;
        [cancelButton addTarget:self action:@selector(cancelBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [actionView addSubview:cancelButton];
        
    }
    return self;
}

- (void)selectPhoteBtnClick
{
    YCLog(@"从手机相册中选择...");
    UIViewController *viewController = [[UIApplication sharedApplication].keyWindow.rootViewController.childViewControllers lastObject];
   [MultipleImagePickerViewController presentMutipleImagePickerControllerAtViewController:viewController maxNumberOfImages:1 completion:^(NSArray *images) {
        for (NSDictionary *obj in images)
        {
            UIImage *image = obj[MutipleOriginImageKey];
            !_selectPhoteBlock? : _selectPhoteBlock(image);
            [self hide];//选择完弹回
        }
       
   } cancel:^{ // 直接取消,不选了
       [self hide];
   }];
}

- (void)takePhoteBtnClick
{
    YCLog(@"拍照...");
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    imagePicker.delegate = self;
    imagePicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:imagePicker animated:YES completion:nil];
}

- (void)cancelBtnClick
{
    YCLogFunc
    [UIView animateWithDuration:0.25 animations:^{
        self.actionView.frame = CGRectMake(0, kScreenHeight, kScreenWidth, 255);
    } completion:^(BOOL finished) {
        [self.actionBackView removeFromSuperview];
        [self removeFromSuperview];
        !_cancelBlock?:_cancelBlock();
    }];
}


- (void)show
{
    [UIView animateWithDuration:0.25 animations:^{
        self.actionView.frame = CGRectMake(0, kScreenHeight - 255, kScreenWidth, 255);
    }];

}

- (void)hide
{
    [self cancelBtnClick];
}

#pragma mark - UIImagePickerController
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    image = [image yc_fixOrientationImage];//处理照片方向问题
    if(!image)
    {
        !_taktePhotoBlock?:_taktePhotoBlock(image);
        [picker dismissViewControllerAnimated:YES completion:nil];
        [self hide];//不成功
        return;
    }
    [UIApplication sharedApplication].keyWindow.rootViewController.navigationItem.rightBarButtonItem.enabled = YES;
    [picker dismissViewControllerAnimated:YES completion:nil];
    !_taktePhotoBlock?:_taktePhotoBlock(image);
    [self hide];//取得相片弹回
}
// 取消,没使用照片
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:YES completion:nil];
    [self hide];
}

@end
